package com.loie.xcx.service.impl;

import com.loie.xcx.mapper.ApprovalMapper;
import com.loie.xcx.mapper.FkblMapper;
import com.loie.xcx.mapper.SqlMapper;
import com.loie.xcx.model.bankdto.Pf_notifiy_loanBO;
import com.loie.xcx.model.bankdto.PftypeBO;
import com.loie.xcx.model.bankdto.Shbank_BillNotifiesLineBO;
import com.loie.xcx.model.bankdto.Shbank_OrdrRgst;
import com.loie.xcx.model.dto.ApplyPaymentDTO;
import com.loie.xcx.service.ApprovalService;
import com.loie.xcx.service.FkblService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author zhl
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class FkblServiceImpl implements FkblService {
    final FkblMapper fkblMapper;
    final SqlMapper sqlMapper;
    private static final Logger paramLogger = LoggerFactory.getLogger("banklog");

    @Override
    public
    Shbank_OrdrRgst getshbank_yforder(String order_no, String billNotifyNo) throws ParseException {
        //测试数据
        Shbank_OrdrRgst shbank_ordrRgst=fkblMapper.getshbank_yforder(order_no);
        //shbank_ordrRgst.setProjectId(6625325769688092672L);
        shbank_ordrRgst.setProjectId(6869581502747320320L);

        shbank_ordrRgst.setCoreCompanyName("宁波前程家居股份有限公司");



        List<Shbank_BillNotifiesLineBO> billNotifiesLineBOList=fkblMapper.getshbank_fplist(billNotifyNo);
//        for (int i = 0, n = billNotifiesLineBOList.size(); i < n; i++) {
//            //billNotifiesLineBOList.get(i).setInvoiceType("NORMAL");
//            //billNotifiesLineBOList.get(i).setVerifyCode("666666");
//            billNotifiesLineBOList.get(i).setNonTaxAmount(107.96);
//        }
        shbank_ordrRgst.setInvoices(billNotifiesLineBOList);
        return shbank_ordrRgst;
        //return null;
    }

    @Override

    public Integer updateshbank_OrdrRgst_checkstate(String sourceAssetNo,String msg,String checkstr ){
        String state="";
        //if (msg.equals("执行成功") && rejectReason.isEmpty()) {
        if (msg.equals("已上链")) {state="已审核";}
        else if(msg.equals("废弃")) state="废弃";
        else if(msg.equals("已注销")) state="已注销";
        else if (msg.equals("已放款")) state="";
        else  state="审核中";
        return fkblMapper.updateshbank_OrdrRgst_checkstate(state,sourceAssetNo, msg, checkstr);

    }

    @Override
    public
    PftypeBO gettype(String code, String field_enname) {
        return fkblMapper.gettype(code,field_enname);
    }

    @Override
    public
    Integer updateshbank_OrdrRgst_state(String orderNo, String state, String statusMsg, String sourceAssetNo, String importSerialNo) {
        return fkblMapper.updateshbank_OrdrRgst_state(orderNo,state,statusMsg,sourceAssetNo,importSerialNo);
    }

    @Override
    public
    String add_notifiy_loan(Pf_notifiy_loanBO bo) {
        Map<String, Object> params = new HashMap<>();
        params.clear();
        params.put("sql", "select count(*) from pf_notifiy_loan where orderSeqNo=#{orderSeqNo} and bank_name=#{bankname}  and loanStatus=N'已放款'");
        params.put("orderSeqNo", bo.getOrderSeqNo());
        params.put("bankname", bo.getBankname());

        Integer i =sqlMapper.sqlcount(params);
        Integer count=0;
        String resultstr="";
        resultstr="记录已存在，已放款，忽略";
        if (i==0) {
            params.clear();
            params.put("sql", "select count(*) from pf_notifiy_loan where orderSeqNo=#{orderSeqNo} and bank_name=#{bankname}");
            params.put("orderSeqNo", bo.getOrderSeqNo());
            params.put("bankname", bo.getBankname());

            Integer j =sqlMapper.sqlcount(params);



            if (j == 0) {
                bo.setRecordId(UUID.randomUUID().toString().toLowerCase());
                count=fkblMapper.add_notifiy_loan(bo);
                resultstr="新增上银放款记录，orderSeqNo="+bo.getOrderSeqNo();
            } else {

                count=fkblMapper.update_notifiy_loan(bo);
                resultstr="修改上银放款记录，orderSeqNo="+bo.getOrderSeqNo();
            }
            paramLogger .info("更新上银资产编号:"+bo.getOrderSeqNo()+"订单状态");


        }


        return resultstr;
    }

    @Override
    public
    Integer update_applypayment_fkzt(ApplyPaymentDTO applyPaymentDTO) {
        return fkblMapper.update_applypayment_fkzt(applyPaymentDTO);
    }

    @Override
    public
    List<ApplyPaymentDTO> getApplyno() {
        return fkblMapper.getApplyno();
    }

}
